﻿using BCGM.MedicalWisdom.Controllers;
using BCGM.MedicalWisdom.IServices;
using BCGM.MedicalWisdom.Model;
using BCGM.MedicalWisdom.Model.Models;
using BCGM.MedicalWisdom.Model.ViewModels.Order;
using Microsoft.AspNetCore.Mvc;
using System.Linq.Expressions;

namespace BCGM.MedicalWisdom.Api.Controllers
{
    /// <summary>
    /// 后台管理系统-药品订单管理模块
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    //[Authorize(Permissions.Name)]
    public class DrugOrderController : BaseApiController
    {
        /// <summary>
        /// 服务器接口，因为是模板生成，所以首字母是大写的，自己可以重构下
        /// </summary>
        private readonly IDrugOrderServices _drugOrderServices;

        public DrugOrderController(IDrugOrderServices DrugOrderServices)
        {
            _drugOrderServices = DrugOrderServices;
        }
        /// <summary>
        /// 药品订单信息-分页显示
        /// </summary>
        /// <param name="page"></param>
        /// <param name="key"></param>
        /// <param name="intPageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<PageModel<DrugOrder>>> Get(int page = 1, string key = "", int intPageSize = 50)
        {
            if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
            {
                key = "";
            }

            Expression<Func<DrugOrder, bool>> whereExpression = a => true;

            return new MessageModel<PageModel<DrugOrder>>()
            {
                msg = "获取成功",
                success = true,
                response = await _drugOrderServices.QueryPage(whereExpression, page, intPageSize)
            };

        }
        /// <summary>
        /// 根据Id查询订单数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<MessageModel<DrugOrder>> Get(string id)
        {
            return new MessageModel<DrugOrder>()
            {
                msg = "获取成功",
                success = true,
                response = await _drugOrderServices.QueryById(id)
            };
        }

        /// <summary>
        /// 药品订单显示
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderByFields"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<List<DrugOrderOutputDto>>> GetDrugOrderSql()
        {
            return new MessageModel<List<DrugOrderOutputDto>>()
            {
                msg = "获取成功",
                success = true,
                response = await _drugOrderServices.GetDrugOrderSql(),
            };
        }

        /// <summary>
        /// 添加药品订单表数据
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<string>> Post([FromBody] DrugOrder request)
        {
            var data = new MessageModel<string>();

            var id = await _drugOrderServices.Add(request);
            data.success = id > 0;
            if (data.success)
            {
                data.response = id.ObjToString();
                data.msg = "添加成功";
            }

            return data;
        }

        /// <summary>
        /// 药品添加购物车
        /// </summary>
        /// <param name="drugCarDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<bool>> AddDrugCar([FromBody] DrugCarDto drugCarDto)
        {
            var id = await _drugOrderServices.AddDrugRedis(drugCarDto);
            return Success(id, "成功");
        }

        /// <summary>
        /// 获取用户购物车信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<List<DrugCarDto>>> GetDrugCar(string userId)
        {
            return new MessageModel<List<DrugCarDto>>()
            {
                msg = "获取成功",
                success = true,
                response = await _drugOrderServices.GetDrugCarRedis(userId),
            };
        }
        /// <summary>
        /// 修改用户购物车药品购买数量
        /// </summary>
        /// <param name="drugCarDto"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<MessageModel<bool>> UptDrugCar([FromBody] DrugCarDto drugCarDto)
        {
            var id = await _drugOrderServices.UptDrugCarRedis(drugCarDto);
            return Success(id, "成功");
        }
        /// <summary>
        /// 删除用户购物车
        /// </summary>
        /// <param name="drugCarDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<bool>> DeleteDrugCar([FromBody] DrugCarDto drugCarDto)
        {
            var id = await _drugOrderServices.RemoveDrugCarRedis(drugCarDto);
            return Success(id, "成功");
        }
        /// <summary>
        /// 修改药品订单信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<MessageModel<string>> Put([FromBody] DrugOrder request)
        {
            var data = new MessageModel<string>();
            data.success = await _drugOrderServices.Update(request);
            if (data.success)
            {
                data.msg = "更新成功";
                data.response = request?.DrugOrderId.ObjToString();
            }

            return data;
        }
        /// <summary>
        /// 删除药品订单信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<MessageModel<string>> Delete(int id)
        {
            var data = new MessageModel<string>();
            var model = await _drugOrderServices.QueryById(id);
            model.IsDeleted = true;
            //data.success = await _departmentServices.Update(model);
            if (data.success)
            {
                data.msg = "删除成功";
                data.response = model?.DrugOrderId.ObjToString();
            }

            return data;
        }
    }
}